package com.example.weather;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class MyCitiesFile
{
	private final Context context;
	private final static String FILE_NAME = "cities.txt";
	static ArrayList<MyCity> MyCitieslist;

	/*
	规定文件内只储存城市+城市代码，一行储存一个城市，将打开程序就显示的城市放在第一行
	北京#01234
	 */
	public MyCitiesFile(Context context)
	{
		this.context = context;
		//this.add_cities();
	}

	public String get_cities_file()
	{
		//if(this.context==null)return null;
		File file = new File(this.context.getFilesDir() + "/" + FILE_NAME);
		if (!file.exists())
		{
			return "";
		}
		try
		{

			FileInputStream is = new FileInputStream(this.context.getFilesDir() + "/" + FILE_NAME);
			byte[] bytes;
			bytes = new byte[is.available()];
			is.read(bytes);
			String cities = new String(bytes,"gbk");
			//Log.e("TAG", "get_cities_file 1: " + cities);
			is.close();
			//Log.e("TAG", "get_cities_file 2: ");
			return cities;
		}
		catch (IOException e)
		{
			Log.e("TAG", "get_cities_file 3: " + e.getMessage());
			return null;
		}
	}

	//调用此函数从文件中加载城市
	public boolean add_cities()
	{
		//if(this.context==null)return false;
		String cities = get_cities_file();
		MyCitieslist = new ArrayList<>();
		if (cities == null)
		{
			return false;
		}
		else if(cities.equals(""))
		{
			return true;
		}
		String[] name_number;
		//Log.e("TAG", "add_cities 1: ");
		for (String city : cities.split("\n"))
		{
			//Log.e("TAG", "add_cities: "+JaoYan.jy_email(city));
			if (city == null || city.equals("")||!JaoYan.jy_email(city))
			{
				continue;
			}
			//Log.e("TAG", "add_cities 5: "+city);
			name_number = city.split("#");
			//Log.e("TAG", "add_cities 2: "+name_number.length);
			MyCitieslist.add(new MyCity(name_number[0], name_number[1]));
			//Log.e("TAG", "add_cities 3: " + name_number[0]+name_number[1]);
		}
		//Log.e("TAG", "add_cities 4: ");
		return true;
	}


	public boolean save_cities()
	{
		//if(this.context==null)return false;
		String data = "";
		File file = new File(this.context.getFilesDir() + "/" + FILE_NAME);
		//Log.e("TAG", "save_cities  1: ");
		if (file.exists())
		{
			if (!file.delete())
			{
				Log.e("TAG", "save_cities  4: 删除旧文件失败");
				return false;
			}
		}
		//Log.e("TAG", "save_cities  5: ");
		//在保存之前去重
		delete_repetitive();
		for (int i = 0; i < MyCitieslist.size(); i++)
		{

			data = data + MyCitieslist.get(i).name + "#" + MyCitieslist.get(i).number + "\n";
		}
		try
		{
			//Log.e("TAG", "save_cities  6: ");
			FileOutputStream is = new FileOutputStream(this.context.getFilesDir() + "/" + FILE_NAME);
			is.write(data.getBytes("GBK"));
			is.flush();
			is.close();
			return true;
		}
		catch (IOException e)
		{
			Log.e("TAG", "save_cities : 文件保存失败" + e.getMessage());
			return false;
		}
	}

	public void delete_repetitive()
	{
		//if(this.context==null)return ;
		int i=0;
		while(i<MyCitieslist.size())
		{
			int j=i+1;
			while(j<MyCitieslist.size())
			{
				if(MyCitieslist.get(i).number.equals(MyCitieslist.get(j).number))
				{
					MyCitieslist.remove(j);
				}
				else
				{
					//动态数组，删掉一个之后下个会补齐，所以接着检查
					j=j+1;
				}
			}
			i=i+1;
		}
	}


}
